<?php

declare(strict_types=1);

namespace App\Controller\jump;
use App\Controller\BaseController;
use App\Model\Applet;
use App\Model\DataSpread;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\Di\Annotation\Inject;
use App\Common\Response\SystemCode;
use App\Service\jump\JumpService;
use Hyperf\View\RenderInterface;

#[AutoController]
class JumpController  extends BaseController
{

    #[Inject]
    private JumpService $jumpService;
    /**
     * 推广列表
     */
    public function Index($key,RenderInterface $render)
    {


        $spreadInfo = DataSpread::query()->where('route_key',$key)->first();
        if(!empty($spreadInfo)){

            $appletInfo = Applet::query()->where(['id' => $spreadInfo->applet_id])->select(['id','name','app_id','cover','type'])->first();
            if(!empty($appletInfo)){

                if($appletInfo->type == 2){
                    $appletInfo =  Applet::query()->where(['id' => 10001])->select(['id','name','app_id','cover'])->first();
                }
                $urlInfo = $this->jumpService->getUrl($appletInfo->id,$spreadInfo);

                if(!empty($urlInfo)){
                    return $render->render('jump',['cover' => $appletInfo->cover,'name' => $appletInfo->name,'url' => $urlInfo]);
                }
            }
        }
        return 'error';
    }
}